package com.i8office.android.zhihuixinwu.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cyan.tools.base.BaseViewHolder;
import com.cyan.tools.base.RecyclerArrayAdapter;
import com.i8office.android.zhihuixinwu.R;
import com.i8office.android.zhihuixinwu.bean.WorkSheet;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by pc on 2018/7/7.
 */

public class AttachmentAdapter extends RecyclerArrayAdapter<WorkSheet.AttachmentsBean> {
    private OnItemClick onItemClick;

    public AttachmentAdapter(Context context) {
        super(context);
    }

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new ViewHolder(parent);
        }
        return new TViewHolder(parent);
    }

    @Override
    public int getViewType(int position) {
        return getData().get(position).getType();
    }

    class ViewHolder extends BaseViewHolder<WorkSheet.AttachmentsBean> {
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.dept_name)
        TextView deptName;
        @BindView(R.id.item_layout)
        View itemLayout;

        public ViewHolder(ViewGroup parent) {
            super(parent, R.layout.item_attachment);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void setData(final WorkSheet.AttachmentsBean data) {
            name.setText(data.getAttFileName());
            itemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClick != null) {
                        onItemClick.onItemClick(getData().indexOf(data));
                    }
                }
            });
            deptName.setText(TextUtils.isEmpty(data.getDeptName())?"":data.getDeptName());
        }
    }

    class TViewHolder extends BaseViewHolder<WorkSheet.AttachmentsBean> {
        @BindView(R.id.name)
        TextView name;

        public TViewHolder(ViewGroup parent) {
            super(parent, R.layout.item_attatchment_header);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void setData(final WorkSheet.AttachmentsBean data) {
            name.setText(data.getAttFileName());
            name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClick != null) {
                        onItemClick.onItemClick(getData().indexOf(data));
                    }
                }
            });
        }
    }

    public interface OnItemClick {
        void onItemClick(int position);
    }
}
